//
//  PlushButtonSubclass.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/3/28.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "PlushButtonSubclass.h"
#import "CYLTabBarController.h"
#import "B_ReleaseView.h"

@implementation PlushButtonSubclass

+ (void)load {
    //请在 `-[AppDelegate application:didFinishLaunchingWithOptions:]` 中进行注册，否则iOS10系统下存在Crash风险。
    //[super registerPlusButton];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.adjustsImageWhenHighlighted = NO;
    }
    return self;
}

//上下结构的 button
- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 控件大小,间距大小
    // 注意：一定要根据项目中的图片去调整下面的0.7和0.9，Demo之所以这么设置，因为demo中的 plusButton 的 icon 不是正方形。
    CGFloat const imageViewEdgeWidth   = self.bounds.size.width * 0.7;
    CGFloat const imageViewEdgeHeight  = imageViewEdgeWidth;
    
    CGFloat const centerOfView    = self.bounds.size.width * 0.5;
    CGFloat const labelLineHeight = self.titleLabel.font.lineHeight;
    CGFloat const verticalMargin  = (self.bounds.size.height - labelLineHeight - imageViewEdgeHeight) * 0.5;
    
    // imageView 和 titleLabel 中心的 Y 值
    CGFloat const centerOfImageView  = verticalMargin + imageViewEdgeHeight * 0.5;
    CGFloat const centerOfTitleLabel = imageViewEdgeHeight  + verticalMargin * 2 + labelLineHeight * 0.5 + 5;
    
    //imageView position 位置
    self.imageView.bounds = CGRectMake(0, 0, imageViewEdgeWidth, imageViewEdgeHeight);
    self.imageView.center = CGPointMake(centerOfView, centerOfImageView);
    
    //title position 位置
    self.titleLabel.bounds = CGRectMake(0, 0, self.bounds.size.width, labelLineHeight);
    self.titleLabel.center = CGPointMake(centerOfView, centerOfTitleLabel);
}

+ (id)plusButton {
    PlushButtonSubclass *button = [[PlushButtonSubclass alloc] init];
    UIImage *buttonImage = [UIImage imageNamed:@"B_home_release"];
    [button setImage:buttonImage forState:UIControlStateNormal];
//    [button sizeToFit]; // or set frame in this way `button.frame = CGRectMake(0.0, 0.0, 250, 100);`
    button.frame = CGRectMake(0.0, 0.0, 70, 90);
    //    button.backgroundColor = [UIColor redColor];
    
    // if you use `+plusChildViewController` , do not addTarget to plusButton.
    [button addTarget:button action:@selector(clickPublish:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}
- (void)clickPublish:(UIButton *) btn {
    NSLog(@"按钮点击了");
    if (getUserId()) {
        B_ReleaseView * b = [[B_ReleaseView alloc] init];
        b.frame = btn.frame;
        [b show];
    }
//    [UIView animateWithDuration:0.2 animations:^{
//        btn.transform = CGAffineTransformMakeRotation(M_PI_4);
//    } completion:^(BOOL finished) {
//        btn.transform = CGAffineTransformMakeRotation(0);
//    }];
}

/**
 高于tabbar的高度
 
 @param tabBarHeight tabbar 的高度
 @return 返回
 */
+ (CGFloat)multiplierOfTabBarHeight:(CGFloat)tabBarHeight {
    return 0.4;
}
//+ (UIViewController *)plusChildViewController {
//    UIViewController *plusChildViewController = [[UIViewController alloc] init];
//    plusChildViewController.view.backgroundColor = [UIColor redColor];
//    plusChildViewController.navigationItem.title = @"PlusChildViewController";
//    UIViewController *plusChildNavigationController = [[UINavigationController alloc]
//                                                       initWithRootViewController:plusChildViewController];
//    return plusChildNavigationController;
//}
//
//+ (NSUInteger)indexOfPlusButtonInTabBar {
//    return 1;
//}

@end
